<template>
  <div class="app-container">
    <el-form
      ref="postForm"
      :model="postForm"
      :rules="postRule"
    >
      <el-form-item label="问题描述:" label-width="100px" prop="title" style="width: 80%">
        <el-input
          ref="title"
          v-model="postForm.title"
          type="textarea"
          placeholder="回复..."
          maxlength="100"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <div class="submit-container">
      <el-button type="primary" style="width: 100px" @click="handleSubmit">
        提交
      </el-button>
      <el-button style="margin-left: 20px" @click="goBack('postForm')">
        返回
      </el-button>
    </div>
  </div>
</template>

<script>
import { createIssue } from '@/api/issue'

export default {
  name: 'IssueDetail',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateTitle = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入问题描述'))
      }
      if (value.length > 100) {
        callback(new Error('问题描述最多100字'))
      }
      callback()
    }
    return {
      postForm: {
        postid: 0,
        title: ''
      },
      postRule: {
        title: [{ required: true, trigger: 'blur', validator: validateTitle }]
      }
    }
  },
  methods: {
    // 提交问题描述
    handleSubmit() {
      createIssue(this.postForm).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.$router.push('/issue/list')
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 返回
    goBack(postForm) {
      this.$refs[postForm].resetFields()
      this.$router.push('/issue/list')
    }
  }
}
</script>

<style lang="scss" scoped>
.submit-container {
  margin: 40px;
}
</style>
